package com.hisicom.scqxspxcjc.sceneCheck.report.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.hisicom.scqxspxcjc.base.dao.BaseRepository;
import com.hisicom.scqxspxcjc.base.service.BaseService;
import com.hisicom.scqxspxcjc.common.file.serivce.FileService;
import com.hisicom.scqxspxcjc.sceneCheck.report.domain.Report;
import com.hisicom.scqxspxcjc.sceneCheck.report.exception.ReportException;
import com.hisicom.scqxspxcjc.sceneCheck.report.repostion.ReportRepository;
import com.hisicom.scqxspxcjc.sceneCheck.resultDecision.service.ResultDecisionBusinessService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author zhonggls
 * @date 2020/12/21 14:12
 * @Explain
 */
@Service
public class ReportService implements BaseService<Report, String> {
    @Autowired
    private ReportRepository reportRepository;
    @Autowired
    private FileService fileService;
    @Autowired
    private ReportInspService reportInspService;
    @Autowired
    private ResultDecisionBusinessService resultDecisionBusinessService;

    @Override
    public BaseRepository<Report, String> getRepository() {
        return reportRepository;
    }

    @Override
    public List<Report> findAll() {
        return getRepository().findAll(Sort.by(Sort.Direction.fromString("asc"), "sort"));
    }

    /**
     * 通过组id获取"现场检查"栏对象集合
     *
     * @param groupId 分组id
     * @return "现场检查"栏对象集合
     */
    public List<Report> findByGroupId(String groupId) {
        return reportRepository.findByGroupId(groupId);
    }

//    public List<Report> findByGroupIdIs(List<String> groupIds) {
//        String[] arr = groupIds.toArray(new String[groupIds.size()]);
//        return reportRepository.findByGroupIdIs(arr);
//    }

    public List<Report> findAllByGroupIdIs(Set<String> groupIds) {
        List<Report> reportList = new ArrayList<>();
        for (String element : groupIds) {
            List<Report> reports = reportRepository.findByGroupId(element);
            if (ObjectUtil.isNotEmpty(reports)) {
                reportList.add(reports.get(0));
            }
        }
        if (ObjectUtil.isNotEmpty(reportList)) {
            reportList.forEach(report -> {
                report.setJcryList(reportInspService.findByZjzxhId(report.getId()));
                report.setResultDecisionList(resultDecisionBusinessService.findByGroupIdOrderBySort(report.getGroupId()));
                report.setFileBeanList(fileService.findByFkBusinessId(report.getGroupId()));
            });
        }
        return reportList;
    }

    /**
     * 保存现场检查报告
     *
     * @param report "现场检查"对象集合数据
     * @return 成功/失败
     */
    public Report saveInfo(Report report) {
        if (StrUtil.isBlank(report.getGroupId())) {
            throw new ReportException("分组id不能为空");
        }

        if (StrUtil.isNotBlank(report.getId())) {
            Report reportOld = findById(report.getId());
            BeanUtils.copyProperties(report, reportOld);
            return save(reportOld);
        } else {
            List<Report> reportList = reportRepository.findByGroupId(report.getGroupId());
            if (ObjectUtil.isNotEmpty(reportList)) {
                throw new ReportException("分组id出现重复");
            }
//            reportRepository.deleteByGroupId(report.getGroupId());
            return save(report);
        }
    }
}
